﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;

namespace Charities
{
    public partial class Needs : Form
    {
        int worthyId;
        Person person = new Person();
        ModelContainer ctx = new ModelContainer();
        int userid;
        Form fo = null;
        public bool is_loaded = false;
        public Needs(int uid,int id,Form m)
        {
            fo = m;
            userid = uid;
            this.MdiParent = m;
            worthyId = id;
            InitializeComponent();
            
        }

        private void Needs_Load(object sender, EventArgs e)
        {
            try
            {
                Worthy w = ctx.Worthies.Single(z => z.Id == worthyId);
                Person p = ctx.People.Single(c => c.Id == w.PersonId);
                supplyTypeBindingSource.DataSource = ctx.SupplyTypes.ToList();
                txb_Name.Text = p.FirstName + " " + p.FatherName + " " + p.LastName;
                txb_worthyId.Text = worthyId.ToString();
                txb_inspector.Text = w.Inspector.Name;
                try
                {
                    var supply = ctx.SupplyTypes.ToList().First();
                    txb_description.Text = "";
                    if (ctx.Needs.Where(c => c.WorthyId == worthyId && c.SupplyTypeId == supply.Id).Count() > 0)
                    {
                        Need n = ctx.Needs.Where(c => c.WorthyId == worthyId && c.SupplyTypeId == supply.Id).First();
                        txb_description.Text = n.Description;
                    }
                }
                catch (Exception)
                {

                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                
            }

            // add this form to window list
            ((Forms.FormMDI)fo).addFormToWindowsList(this);
            is_loaded = true;
        }
        Need need=new Need();
        private void lstbox_needs_SelectedIndexChanged(object sender, EventArgs e)
        {
            var supply = (SupplyType)lstbox_needs.SelectedItem;
            txb_description.ReadOnly = false;
            try
            {
                txb_description.Text = "";
                if (ctx.Needs.Where(c => c.WorthyId == worthyId && c.SupplyTypeId == supply.Id).Count() > 0)
                {
                    Need n = ctx.Needs.Where(c => c.WorthyId == worthyId && c.SupplyTypeId == supply.Id).First();
                    txb_description.Text = n.Description;
                }

            }
            catch (Exception)
            {
                
            }
        }

        private void txb_description_TextChanged(object sender, EventArgs e)
        {
            //Need ne = ctx.Needs.Single(c => c.Id == need.Id);
            //if (txb_description.Text != String.Empty)
            //{
            //    ne.Description = txb_description.Text;
            //    ctx.SaveChanges();
            //}
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txb_description.ReadOnly = true;

            var supply = (SupplyType)lstbox_needs.SelectedItem;
            try
            {
                Need n = ctx.Needs.Single(c => c.SupplyTypeId == supply.Id && c.WorthyId == worthyId);
                 n.Description=txb_description.Text ;
                 n.UpdateDate = DateTime.Now;
                 n.UpdateUId = userid;
                ctx.SaveChanges();
                need = n;
            }
            catch
            {
                if (supply != null)
                {
                    Need n1 = new Need();
                    n1.Description = txb_description.Text;
                    n1.SupplyTypeId = supply.Id;
                    n1.WorthyId = worthyId;
                    n1.InsertDate = DateTime.Now;
                    n1.InsertUId = userid;
                    ctx.Needs.Add(n1);
                    ctx.SaveChanges();
                    need = n1;
                }
            }
            txb_description.Text = String.Empty;
        }

        private void Needs_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
            // remove this form from windows list
            ((Forms.FormMDI)fo).removeFormFromWindowsList(this);
        }

        private void Needs_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
